/**
 * @program: LeetCode
 * @description: LeetCode : 463. 岛屿的周长
 * @author: WXY
 * @create: 2023-01-08 21:55
 * @Version 1.0
 **/
public class Num463_islandPerimeter {
    public static int islandPerimeter(int[][] grid) {
        int ans = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    ans += 4;
                }
                if (i > 0 && grid[i][j] == 1 && grid[i - 1][j] == 1) {
                    ans -= 2;
                }
                if (j > 0 && grid[i][j] == 1 && grid[i][j - 1] == 1) {
                    ans -= 2;
                }
            }
        }
        return ans;
    }
}
